﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Modeling.Common;
using Microsoft.Practices.Modeling.Common.Design;
using Microsoft.Practices.Modeling.Common.Security;
using Microsoft.Practices.Modeling.Common.Utility;
using Microsoft.Practices.Modeling.ExtensionProvider.Utility;
using Microsoft.Practices.Services.ItineraryDsl;
using Microsoft.VisualStudio.Modeling;
using Microsoft.XLANGs.BaseTypes;
using System.CodeDom;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Serialization;
using Microsoft.Practices.Modeling.Services.Design;

namespace BizTalk.ESB.Extensions.Helpers
{
    public class BamTrackingConfigEditor : PropertyGridEditor
    {
        public const string BamActivityDefinition = "BamActivityDefinitionFile";

        public BamTrackingConfigEditor()
        {
        }

        private CodeCompileUnit BuildDomFromProperties(ITypeDescriptorContext context, ref string typeName)
        {
            var manager = BamDefinitionPropertyManager.Create(typeName);
            CodeCompileUnit unit = new CodeCompileUnit();
            CodeNamespace namespace2 = new CodeNamespace(string.Empty);
            typeName = CodeIdentifier.MakeValid(typeName);
            CodeTypeDeclaration declaration = new CodeTypeDeclaration(typeName);
            bool flag = RequireCertificate(context);
            foreach (var property in manager.Manifest.DataMappings)
            {
                try
                {
                    System.Diagnostics.Trace.WriteLine("BamTrackingConfigEditor - Adding Field from BAM XML: " + property.Key);
                    PropertyGridEditor.AddProperty(CodeIdentifier.MakeValid(property.Key), typeof(string), "MakeValid() field name = " + CodeIdentifier.MakeValid(property.Key) + ". Original field name = " + property.Key
                        , (false && flag), declaration.Members);
                    continue;
                }
                catch (ArgumentException exception)
                {
                    Trace.TraceWarning(exception.Message);
                    continue;
                }
            }
            namespace2.Types.Add(declaration);
            unit.Namespaces.Add(namespace2);
            return unit;
        }

        protected override object FillProperties(ITypeDescriptorContext context, object value)
        {
            string inputProperty = EditorUtility.GetInputProperty<string>(context, "BamActivityDefinitionFile");
            Guard.ArgumentNotNullOrEmpty(inputProperty, "BamActivityDefinitionFile");
            return PropertyGridEditor.Initialize(Activator.CreateInstance(PropertyGridEditor.CompileAssemblyFromDom(this.BuildDomFromProperties(context, ref inputProperty)).GetType(inputProperty)), value as string);
        }

        private static bool RequireCertificate(ITypeDescriptorContext context)
        {
            if (SecurityModelUtility.EnforceEncryption(context.Instance as ModelElement))
            {
                ExtensibleModelElement instance = context.Instance as ExtensibleModelElement;
                if (instance != null)
                {
                    EncryptPropertyAttribute attribute = Microsoft.Practices.Modeling.ExtensionProvider.Utility.ReflectionHelper.GetAttribute<EncryptPropertyAttribute>(Microsoft.Practices.Modeling.ExtensionProvider.Utility.ReflectionHelper.GetPropertyInfo(context));
                    if (attribute != null)
                    {
                        X509CertificateContainer containerFromStore = SecurityModelUtility.GetContainerFromStore(instance.ModelElement.Store, attribute.TypeWithX509CertificateContainer);
                        return ((containerFromStore == null) || ((containerFromStore != null) && string.IsNullOrEmpty(containerFromStore.Thumbprint)));
                    }
                }
            }
            return false;
        }

        protected override object SelectedValue(object value, ITypeDescriptorContext context)
        {
            return base.SerializeToString(value);
        }

        protected override string SerializeToStringAppendName(string name)
        {
            return GenericSerializer.XmlEncodeName(name);
        }

        protected override string SerializeToStringAppendValue(object value)
        {
            return GenericSerializer.XmlEncodeValue(value);
        }

        protected override string Title
        {
            get
            {
                return "BAM Tracking Data Mapping Editor";
            }
        }
    }
}
